package com.kh.pajx.sop.service.impl;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;

import org.apache.commons.lang.StringUtils;
import org.apache.struts2.ServletActionContext;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.kh.pajx.sop.base.SOPConfig;
import com.kh.pajx.sop.dao.ISysUserRoleDao;
import com.kh.pajx.sop.dao.school.IHsSchoolInfoDao;
import com.kh.pajx.sop.dao.sys.ISysDepartmentDao;
import com.kh.pajx.sop.dao.user.ISysInsideUserDao;
import com.kh.pajx.sop.dao.user.ISysOutsideUserDao;
import com.kh.pajx.sop.dao.user.impl.SysOutsideUserDaoImpl;
import com.kh.pajx.sop.domain.SOPCurrentUser;
import com.kh.pajx.sop.domain.SysInsideUser;
import com.kh.pajx.sop.domain.SysUserRole;
import com.kh.pajx.sop.service.ISopLoginService;
import com.kh.pajx.sop.service.sys.ISysDeptAreaService;
import com.kh.pajx.sop.util.common.FormatDate;
import com.kh.pajx.sop.util.security.encoder.Md5PwdEncoderUtil;
import com.kh.pajx.sop.util.web.RequestUtils;

/**
 * 用户登入、登出业务逻辑实现类
 * @author:	azzcsimp
 * @Createdate:	2014年7月3日 下午2:54:39
 */
@Service
@Transactional
public class SopLoginServiceImpl implements ISopLoginService {

	@Resource
	private ISysInsideUserDao sysInsideUserDao;
	
	@Resource
	private ISysOutsideUserDao outsideUserDao;
	
	@Resource
	private ISysUserRoleDao sysUserRoleDao;
	
	@Resource 
	private ISysDepartmentDao sysDepartmentDao;
	
	@Resource 
	private IHsSchoolInfoDao hsSchoolInfoDao;
	
	@Resource
	private ISysDeptAreaService deptAreaService;

	@Override
	public SOPCurrentUser login(String account, String password) {
		SOPCurrentUser currentUser = null;
		
		// 获取内部用户对象
		SysInsideUser insideUser = sysInsideUserDao
				.getUserByAccountAndPassword(StringUtils.trim(account),
						Md5PwdEncoderUtil.encrytion(StringUtils.trim(password)));
		
		if(insideUser != null){
			currentUser = new SOPCurrentUser();
			if(insideUser.getIsuStatusFlag() == 1){
				// 设置角色信息
				SysUserRole userRole = sysUserRoleDao.getUserRoleByCode(insideUser.getIsuRoleCode());
				currentUser.setUserRole(userRole);
				// 设置用户组信息
				currentUser.setUserGroup(userRole.getSysUserGroup());
				// 设置部门信息
				currentUser.setDepartment(sysDepartmentDao.getByCode(insideUser.getDeptCode()));
				currentUser.setUserId(insideUser.getIsuId());
				currentUser.setAccount(account);
				currentUser.setPassword(password);
				currentUser.setUserName(insideUser.getIsuName());
				
				if(StringUtils.isNotEmpty(insideUser.getLastLoginTime())){
					currentUser.setLastLoginTime(FormatDate.getMsgSendTime(insideUser.getLastLoginTime()));
				}
				currentUser.setLastLoginIp(insideUser.getLastLoginIp());
	
				
				// ==== 更新用户信息
				insideUser.setLastLoginIp(RequestUtils.getIpAddr(ServletActionContext.getRequest()));
				insideUser.setLastLoginTime(FormatDate.dateTimeDetail());
				sysInsideUserDao.update(insideUser);
				
				currentUser.setInsideUser(insideUser);
				// ==== 获取学校ID列表
				getSclIdList(currentUser);
				
			}else{
				currentUser.setInsideUser(insideUser);
			}
		}

		return currentUser;
	}
	
	private void getSclIdList(SOPCurrentUser currentUser){
		List<String> sclIdList = new ArrayList<String>();
		// ----2014年 目前客服为分组  查看权限为ALL
		// 2015 依据角色的数据权限来控制
		if(!currentUser.isLookAll()){
			if(currentUser.getUserRole().getUsrDataRange()==1){
				// 部门数据访问权限    目前主要是市场部  其他部门后期扩展再维护此处代码
				
				/*
				 * 2014 市场部门经理查看下属绑定的学校
				 * List<String> userIdList = sysInsideUserDao.getUserIdListByDeptCode(currentUser.getInsideUser().getDeptCode());
				if(userIdList!=null && userIdList.size()>0){
					sclIdList = hsSchoolInfoDao.getSclIdListBySale(userIdList);
				}*/
				
				// 获取所属部门的负责区域编码集合
				List<String> areaCodeList = new ArrayList<String>();
				try {
					areaCodeList = deptAreaService.getAreaCodeByDeptCode(currentUser.getDepartment().getDeptCode());
				} catch (Exception e) {
					e.printStackTrace();
				}
				if(areaCodeList.size()>0){
					sclIdList = hsSchoolInfoDao.getSclIdListByAreaCode(areaCodeList);
				}
				
			}else if(currentUser.getUserRole().getUsrDataRange()==2){ 
				// 目前主要针对市场部
				// 如果是市场扩展人员
				if(currentUser.getUserRole().getUsrCode().equals(SOPConfig.CODE_USER_ROLE_EXTEND)){
					if(currentUser.getInsideUser().getManagerFlag().equals("1")){
						// 获取所属部门的负责区域编码集合
						List<String> areaCodeList = new ArrayList<String>();
						try {
							areaCodeList = deptAreaService.getAreaCodeByDeptCode(currentUser.getDepartment().getDeptCode());
						} catch (Exception e) {
							e.printStackTrace();
						}
						if(areaCodeList.size()>0){
							sclIdList = hsSchoolInfoDao.getSclIdListByAreaCode(areaCodeList);
						}
					}else{
						// 普通市场扩展人员、市场维护人员、（普通客服人员、普通工程人员目前为全部）
						sclIdList = hsSchoolInfoDao.getSclIdListBySale(currentUser.getUserId());
					}
				}else if(currentUser.getUserRole().getUsrCode().equals(SOPConfig.CODE_USER_GROUP_MAINTAIN)){
					// 市场扩展人员
					sclIdList = hsSchoolInfoDao.getSclIdListByKeep(currentUser.getUserId());
				}
			}
			
		}else{
			sclIdList=hsSchoolInfoDao.getAllSclId();
		}
		currentUser.setSclIdList(sclIdList);
	}

	
	public Map<String,String> initAllUserMap(String sclId){
		Map<String,String> allUserMap = new HashMap<String, String>();
		
		// ====> 装载内部用户
		List<String[]> isuUserList = sysInsideUserDao.getUserSimple();
		for (String[] array : isuUserList) {
			allUserMap.put(array[0], array[1]);
		}
		
		// ====> 装载外部用户
		List<String[]> osuUserList = outsideUserDao.getUserSimple();
		for (String[] array : osuUserList) {
			allUserMap.put(array[0], array[1]);
		}
		
		if(StringUtils.isNotEmpty(sclId)){
			// ====> 装载教师
			List<String[]> teacherList = outsideUserDao.getTeacherSimple(sclId);
			for (String[] array : teacherList) {
				allUserMap.put(array[0], array[1]);
			}
		}
		
		return allUserMap;
	}
}
